﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Wicresoft.EKA.MVCExtensions;
using Wicresoft.EKA.ITSM.Business;
using Ninject;

namespace Wicresoft.EKA.ITSM.Web.WebPortal.Controllers
{
    public class SMGXJPlanTicketRuleRelatedAPIController : BaseAPIController
    {
        [Inject]
        public ISMGXJPlanTicketRuleRelated SMGXJPlanTicketRuleRelated { get; set; }

        [Inject]
        public ISMGXJPlanObject SMGXJPlanObject { get; set; }

        [Inject]
        public override IUserInfo UserInfo { get; set; }  //每个Controller必须包含的。

        protected override void Init()
        {
            ((IBase)SMGXJPlanTicketRuleRelated).UserIdentify = UserName;
            ((IBase)SMGXJPlanObject).UserIdentify = UserName;
            base.Init();
        }

        protected override void Dispose(bool disposing)
        {
            ((IBase)SMGXJPlanTicketRuleRelated).Dispose();
            ((IBase)SMGXJPlanObject).Dispose();
            base.Dispose(disposing);
        }

        //废弃，这张表不用保存了
        public object Post([FromBody] List<SMGXJPlanTicketRuleRelatedModel> modellist)
        {
            Init();
            string result = string.Empty;
            foreach (var model in modellist)
            {
                model.Submitter = UserName;
                model.ShortDescription = "Create";
                result = SMGXJPlanTicketRuleRelated.Create(model);
                if (result.Contains("Fail"))
                {
                    break;
                }
            }
            return result;
        }

        //废弃，直接拼querystring来查询
        public object GetbyID(string TicketID, string Name,string Type)
        {
            Init();
            return SMGXJPlanTicketRuleRelated.GetList("\'cChr_TicketID\' =\"" + TicketID + "\" AND \'cChr_ObjectID\' =\"" + Name + "\" AND \'cChr_MarkAsDelete\' = \"No\" AND 'cChr_Char03' =\"" + Type + "\"");
        }

        //废弃，不用更新后台，直接拼好传给前台即可
        public object Put(string id, [FromBody] SMGXJPlanTicketRuleRelatedModel model)
        {
            Init();
            string Qual ="\'cChr_TicketID\' =\"" + model.cChr_TicketID + "\" AND \'cChr_ObjectID\' =\"" + id + "\"";
            return  SMGXJPlanTicketRuleRelated.Modify(model,Qual);
        }

        //废弃，不用了
        public object GetList(string TicketID)
        {
            Init();
            SMGXJPlanTicketRuleRelatedModel model = new SMGXJPlanTicketRuleRelatedModel();
            model.cChr_MarkAsDelete = "Yes";
            var list = SMGXJPlanObject.GetList("\'cChr_RelatedTicketID\' =\"" + TicketID + "\" AND \'cChr_MarkAsDelete\' = \"No\"").Select(o=>o.cChr_DeviceName);
            string Qual = "";
            if (list.Count() > 0)
            {
                foreach (var i in list)
                {
                    Qual = Qual + "'cChr_ObjectID' != \"" + i + "\" AND ";
                }
                Qual = Qual + "'cChr_TicketID' = \"" + TicketID + "\"";
                return SMGXJPlanTicketRuleRelated.Modify(model, Qual);
            }
            return "";
        }
    }
}
